﻿using AlibabaCloud.TeaUtil.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;

namespace Acme.BookStore.SMS
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class SMSServices:ApplicationService
    {
        private readonly IConfiguration _configuration;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configuration"></param> 
        public SMSServices(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        [HttpPost("SendSmsAsync")]
        public async Task<bool> SendSmsAsync(string phone)
        {
            // 发送短信逻辑
            // 这里省略了短信发送的具体实现
            // 这里只是打印日志
            var accessKeyId = _configuration["Consul:AccessKeyId"];
            var accessKeySecret = _configuration["Consul:AccessKeySecret"];
            //var regionId = _configuration["Consul:ReigonId"];
             //|| string.IsNullOrEmpty(regionId)
            // 检查短信配置信息是否完整
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(accessKeySecret))
            {
                throw new InvalidOperationException("短信配置信息不完整");
            }
            // 创建Config对象
            var config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                AccessKeyId = accessKeyId,
                AccessKeySecret = accessKeySecret,
                //RegionId = regionId
            };
            // 创建AcsClient实例
            var client = new AlibabaCloud.SDK.Dysmsapi20170525.Client(config);
            // 生成验证码
            Random random = new Random();
            int code = random.Next(100000, 999999);
            // 组装请求对象
            var request = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest
            {
                PhoneNumbers = phone,
                SignName = "阿里云短信测试",
                TemplateCode = "SMS_154950909",
                TemplateParam = "{\"code\":\"" + code + "\"}"
            };

            var runtimeOptions = new RuntimeOptions();

            // 调用短信发送接口
            try
            {
                // 调用短信发送接口
                var response = await client.SendSmsWithOptionsAsync(request, runtimeOptions);
                // 打印返回结果
                if (response.Body.Code == "OK")
                {
                    return true;
                }
                else
                {

                    Console.WriteLine(response.Body.Message);
                    return false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }

        }
    }
}
